package org.wangzhen.spring.event.springdemo;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class CustomerListenerDemo {

    // 自定义spring事件
    static class MyApplicationEvent extends ApplicationEvent {
        public MyApplicationEvent(Object source) {
            super(source);
        }
    }

    static class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {
        @Override
        public void onApplicationEvent(MyApplicationEvent event) {
            System.out.println("接受到了自定义的MyApplicationEvent事件：" + event);
        }
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        context.register(CustomerListenerDemo.class);

        context.addApplicationListener(new MyApplicationListener());
        context.refresh();

        context.publishEvent(new MyApplicationEvent("hello world"));

        context.close();
    }



}
